import physics.Vect;

import java.awt.*;
import java.awt.geom.Point2D;

/**
 * Created by obila_000 on 2014/10/24.
 */
public class Physics {
    /**
     * 一个点绕另一个点旋转一定角度
     * @param rotatePoint 动点
     * @param staticPoint 轴点
     * @param angle 旋转角度,角度制
     * @return 新点
     */
    public static Point rotateByPoint(Point rotatePoint, Point staticPoint, float angle){
        float x1 = (float)rotatePoint.getX();
        float y1 = (float)rotatePoint.getY();
        float x0 = (float)staticPoint.getX();
        float y0 = (float)staticPoint.getY();
        angle=new Float(Math.toRadians(angle));
        float x2=new Float((x1-x0)*Math.cos(angle) +(y1-y0)*Math.sin(angle)+x0);
        float y2=new Float(-(x1-x0)*Math.sin(angle) + (y1-y0)*Math.cos(angle)+y0);

        return new Point((int)x2,(int)y2);
    }

    /**
     * 计算一组点绕特定点旋转一定角度之后的新点
     * @param topX  点x坐标的集合
     * @param topY  点y坐标的集合
     * @param angle 旋转角度
     * @param x  轴点x坐标
     * @param y  轴点y坐标
     * @return
     */
    public static int[][] afterRotate(int[] topX, int[] topY, float angle,int x, int y){
        int[][] newTop = new int[2][topX.length];
        for (int i = 0;i < topX.length; i++){
            Point newPoint = Physics.rotateByPoint(new Point(topX[i],topY[i]),new Point(x,y),angle);
            newTop[0][i] = (int)newPoint.getX();
            newTop[1][i] = (int)newPoint.getY();
        }
        return newTop;
    }

    /**
     * 根据小球速度计算碰撞点
     * @param ballCenter 小球中心坐标
     * @param vx 小球x方向速度
     * @param vy 小球y方向速度
     * @param radius 小球半径
     * @return
     */
    public static Point2D.Float getBallCollisionPoint(Point2D.Float ballCenter, float vx, float vy, float radius){
        Point2D.Float collisionPoint = new Point2D.Float((float)ballCenter.getX(),(float)ballCenter.getY());
        float v = (float)Math.sqrt(vx*vx+vy*vy);
        float deltaX = vx*radius/v;
        float deltaY = vy*radius/v;
        collisionPoint.x = (float)(ballCenter.getX() + deltaX);
        collisionPoint.y = (float)(ballCenter.getY() + deltaY);
        return collisionPoint;
    }

    /**
     * 点与直线的碰撞
     * @param originVect 原来点的速度
     * @param obstaclePoint1 直线定位点1
     * @param obstaclePoint2 直线定位点2
     * @return 点新速度
     */
    public static Vect getBallVecticalAfterCollision(Vect originVect, Point2D.Float obstaclePoint1, Point2D.Float obstaclePoint2){
        Vect newVect = new Vect(originVect.x(),originVect.y());
        if(originVect.y() == 0 && originVect.x() == 0){
            return newVect;
        }
        if(obstaclePoint1.equals(obstaclePoint2)){
            return newVect;
        }

        double angelObstacle = Math.atan((obstaclePoint2.getY()-obstaclePoint1.getY())/(obstaclePoint2.getX()-obstaclePoint1.getX()));
        double angelOriginVect = Math.atan(originVect.y()/originVect.x()) - Math.PI;
        double angelTargetVect = Math.PI - angelOriginVect + 2*angelObstacle;
        double v = Math.sqrt(originVect.x()*originVect.x() + originVect.y()*originVect.y());
        double x = v * Math.cos(angelTargetVect);
        double y = v * Math.sin(angelTargetVect);

        System.out.println("反射速度x="+x+" y="+y);

        return new Vect(x,y);
    }

    /**
     * 计算点到直线距离
     * @param ax A点的x轴坐标
     * @param ay A点的y轴坐标
     * @param bx B点的x轴坐标
     * @param by B点的y轴坐标
     * @param cx C点的x轴坐标
     * @param cy C点的y轴坐标
     * @return a点到bc线段的距离
     */

    public static float checkConfliction(float ax,float ay,float bx,float by,float cx,float cy) {

        float a = 0;
        float b = 0;
        float c = 0;
        float temp = 0;
        float h = 0;//a,b,c三角形的三条边（分别对应A,B,C点），temp是中间值,h是距离

        a = (float)Math.pow((Math.pow((bx - cx), 2) - Math.pow((by - cy), 2)), 1 / 2);
        b = (float)Math.pow((Math.pow((ax - cx), 2) - Math.pow((ay - cy), 2)), 1 / 2);
        c = (float)Math.pow((Math.pow((ax - bx), 2) - Math.pow((ay - by), 2)), 1 / 2);

        temp = ((b - c) * (b + c) / a + a) / 2;
        h = (float)Math.pow((b - temp), (b + temp));

        return h;
    }


    private static Double getLenWithPoints(double p1x, double p1y, double p2x, double p2y) {
        Double length = null;
        length = Math.sqrt(Math.pow(p2x - p1x, 2) + Math.pow(p2y - p1y, 2));
        return length;
    }

    public static Double getLength(double lx1, double ly1, double lx2,
                                   double ly2, double px, double py) {
        Double length = null;
        double b = getLenWithPoints(lx1, ly1, px, py);
        double c = getLenWithPoints(lx2, ly2, px, py);
        double a = getLenWithPoints(lx1, ly1, lx2, ly2);

        if (c + b == a) {// 点在线段上
            length = (double) 0;
        } else if (c * c >= a * a + b * b) { // 组成直角三角形或钝角三角形，投影在point1延长线上，
            length = b;
        } else if (b * b >= a * a + c * c) {// 组成直角三角形或钝角三角形，投影在point2延长线上，
            length = c;
        } else {
            // 组成锐角三角形，则求三角形的高
            double p = (a + b + c) / 2;// 半周长
            double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));// 海伦公式求面积
            length = 2 * s / c;// 返回点到线的距离（利用三角形面积公式求高）
        }
        return length;
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args){
        System.out.println(getBallCollisionPoint(new Point2D.Float(5,5),1.0f,-1.0f,1));
        Vect vect = getBallVecticalAfterCollision(new Vect(1,1),new Point2D.Float(1,0),new Point2D.Float(0,1));
        System.out.println(3&3);
       //System.out.println(checkConfliction(1,1,1,1,1,1));
    }
}
